#include <QApplication>
#include "puzzlewindow.h"
#include "welcomewindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    RecordsManager *recordsManager = new RecordsManager;
    WelcomeWindow *welcomeWindow = new WelcomeWindow(recordsManager); // 改为动态创建
    PuzzleWindow* puzzleWindow = nullptr;



    QObject::connect(welcomeWindow, &WelcomeWindow::startClicked,
                     [&](QRect geometry, int difficulty){
                         // 销毁旧窗口
                         if(puzzleWindow) {
                             puzzleWindow->deleteLater();
                             puzzleWindow = nullptr;
                         }

                         // 创建新窗口
                         puzzleWindow = new PuzzleWindow(difficulty,welcomeWindow,recordsManager);
                         puzzleWindow->setGeometry(geometry);
                         puzzleWindow->show();
                         welcomeWindow->hide(); // 隐藏欢迎界面，而不是关闭
                     });


    // 使用Qt::UniqueConnection避免重复连接
    QObject::connect(puzzleWindow, &PuzzleWindow::destroyed, welcomeWindow, [welcomeWindow]() {
        welcomeWindow->show(); // 直接显示，确保对象有效
    });

    // 连接拼图窗口关闭信号到显示欢迎界面
    QObject::connect(welcomeWindow, &WelcomeWindow::showWelcome, [&]() {
        welcomeWindow->show();

    });

    welcomeWindow->show();
    app.exec();

    // 程序退出时手动释放
    delete welcomeWindow;
    return 0;
}
